package com.autotest.nore.mapper;

import com.autotest.nore.dto.ModuleDTO;
import com.autotest.nore.dto.ModuleListDTO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ModuleMapper {
    void saveModule(ModuleDTO module);
    void saveModuleApi(ModuleDTO module);

    void updateModule(ModuleDTO module);

    void updateModuleApi(ModuleDTO module);

    void updateApiModule(String id ,String name,String mockPrefix);

    void updateCaseModule(String id ,String name);

    void updateCollectionModule(String id ,String name);

    void deleteApiModule(String id);

    void deleteCaseModule(String id);


    ModuleDTO getModuleByParentAndNameFromApiModule(String parentId,String name);
    ModuleDTO getModuleByParentAndNameFromCaseModule(String parentId,String name);
    ModuleDTO getModuleByParentAndNameFromCollectionModule(String parentId,String name);



    List<ModuleListDTO> getModuleList(String moduleType, String projectId);
    List<ModuleListDTO> getCollectionModuleList(String projectId);

    @Select("SELECT id,name,parent_id,project_id,mock_prefix FROM api_module WHERE project_id = #{projectId} ORDER BY name ASC")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "parentId", column = "parent_id"),
            @Result(property = "projectId", column = "project_id"),
            @Result(property = "mockPrefix", column = "mock_prefix"),
            @Result(property = "apiList", column = "id", javaType = List.class, many = @Many(select = "com.autotest.nore.mapper.ApiMapper.getApisByModuleId")),
            @Result(property = "children", column = "id", javaType = List.class, many = @Many(select = "com.autotest.nore.mapper.ModuleMapper.getChildrenModules"))
    })
    List<ModuleListDTO> getModulesByProjectId(@Param("projectId") String projectId);

    @Select("SELECT * FROM api_module WHERE parent_id = #{parentId}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "parentId", column = "parent_id"),
            @Result(property = "projectId", column = "project_id"),
            @Result(property = "mockPrefix", column = "mock_prefix"),
            @Result(property = "apiList", column = "id", javaType = List.class, many = @Many(select = "com.autotest.nore.mapper.ApiMapper.getApisByModuleId")),
            @Result(property = "children", column = "id", javaType = List.class, many = @Many(select = "com.autotest.nore.mapper.ModuleMapper.getChildrenModules"))
    })
    List<ModuleDTO> getChildrenModules(@Param("parentId") String parentId);



}